import cn.golxen.barcode.entity.BarcodeInfo;
import cn.golxen.barcode.utli.APIUtlis;
import cn.golxen.commons.exception.BarcodeException;
import cn.golxen.commons.exception.BarcodeInfoException;
import org.json.JSONObject;
import org.junit.Test;

public class test {
    @Test
    public void test() throws Exception{
//        String res=new ShowApiRequest("http://route.showapi.com/2275-4","1066592","b8eb4b1c82c14db5823f6ec4a0b11519")
//                .addTextPara("image_url","http://bt.golxen.cn:15357/down/qNpUS2ziYkV5")
//                .post();
//        System.out.println(res);
//        try {
            APIUtlis apiUtlis=new APIUtlis();
//            String res=apiUtlis.GetBarcode("http://bt.golxen.cn:15357/down/SUrjCJUE5nmM");
            String res=apiUtlis.GetBarcode("http://bt.golxen.cn:15357/down/Rm11QT65Avwh");
            System.out.println(res);
            JSONObject jsonObject=new JSONObject(res);
            if(jsonObject.getInt("showapi_res_code")==-1)
                throw new BarcodeException();
            JSONObject showapi_res_body=jsonObject.getJSONObject("showapi_res_body");
            int ret_code=showapi_res_body.getInt("ret_code");
            if (ret_code!=0)
                throw new BarcodeException();
            String ret_body=showapi_res_body.getString("ret_body");
            JSONObject jsonObject1=new JSONObject(ret_body);
            if(jsonObject1.getInt("codes_result_num")==0)
                throw new BarcodeException();
            String text=jsonObject1.getJSONArray("codes_result").getJSONObject(0).getJSONArray("text").getString(0);
            System.out.println(ret_code+text);
//        }
//        catch (Exception e){
//
//        }

    }

    @Test
    public void test2()throws Exception{
//        String res=new ShowApiRequest("http://route.showapi.com/66-22","1066592","b8eb4b1c82c14db5823f6ec4a0b11519")
//                .addTextPara("code","6921168558032")
//                .post();
//        System.out.println(res);
        BarcodeInfo barcodeInfo = new BarcodeInfo();
        APIUtlis apiUtlis=new APIUtlis();
        String resGoods=apiUtlis.GetGoodsInfo("6921168558032");
        JSONObject jsonObject2=new JSONObject(resGoods).getJSONObject("showapi_res_body");
        if(!jsonObject2.getString("ret_code").equals("0"))
            throw new BarcodeInfoException();
        barcodeInfo.setCode("6921168558032");
        barcodeInfo.setGoodsName(jsonObject2.getString("goodsName"));
        barcodeInfo.setTrademark(jsonObject2.getString("trademark"));
        if (!jsonObject2.getString("price").equals(""))
            barcodeInfo.setPrice(Double.parseDouble(jsonObject2.getString("price")));
        if (!jsonObject2.getString("img").equals(""))
            barcodeInfo.setUrl(jsonObject2.getString("img"));
        System.out.println(barcodeInfo);
    }

//    @Test
//    public void test3()throws Exception{
//
//        BarcodeServiceImpl qrcodeService=new BarcodeServiceImpl();
//
//        Java2DFrameConverter java2dConverter = new Java2DFrameConverter();  //用以完成Frame到BufferedImage的格式转换
//
//        /**
//         * 摄像头资源
//         */
//        OpenCVFrameGrabber grabber=new OpenCVFrameGrabber(0);
//        grabber.start();
//
//        Frame frame = grabber.grabFrame();
//        BufferedImage bImage =java2dConverter.getBufferedImage(frame);
//        String url = qrcodeService.QrParse(bImage);
//        System.out.println(url);
//        Thread.sleep(1000);
//
//    }


}
